package facesdk

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	// "log"
	"errors"
	"net/http"
)

const (
	FACEURL    = "http://appnodebyyang.qiniudn.com/ct2.jpg"
	FACEKEY    = "507b3151f5669a67928b8ea30136fc29"
	FACESECRET = "OtvS7fI3V9G4VUrwKUfIHJJHnj9t77IZ"
	APIURL     = "apicn.faceplusplus.com"
)

type Group struct {
	GroupId     string
	AddedPerson int
	GroupName   string
}

func CreateGroup(groupname string) (groupId string, err error) {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/group/create?api_key=%s&api_secret=%s&group_name=%s", FACEKEY, FACESECRET, groupname)
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}
	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	fmt.Println(string(buf))
	group := new(Group)
	err = json.Unmarshal(buf, group)
	return group.GroupId, err

}

type Person struct {
	AddedFace  int
	AddedGroup int
	PersonId   string
	PersonName string
	Tag        string
}

// 新建一个人
func CreatePerson(groupname, personname string) (personId string, err error) {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/person/create?api_key=%s&api_secret=%s&tag=demotest&person_name=%s&group_name=%s", FACEKEY, FACESECRET, personname, groupname)
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	person := new(Person)
	err = json.Unmarshal(buf, person)

	fmt.Println(person)
	fmt.Println(string(buf))

	return person.PersonId, err
}

// 删除一个人
func DeletePerson(psersonname string) (err error) {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/person/delete?api_secret=%s&api_key=%s&person_name=%s", FACEKEY, FACESECRET, psersonname)

	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	type d struct {
		Deleted int
		Success bool
	}

	ok := new(d)
	err = json.Unmarshal(buf, ok)

	if err != nil {
		return err
	}
	if ok.Success {
		return nil
	}
	return errors.New("DeleteFailed")
}

func AddFace(personname, faceId string) (err error) {

	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/person/add_face?api_secret=%s&face_id=%s&api_key=%s&person_name=%s", FACESECRET, faceId, FACEKEY, personname)

	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	type a struct {
		Added   int
		Success bool
	}
	ok := new(a)
	err = json.Unmarshal(buf, ok)

	if err != nil {
		return err
	}
	if ok.Success {
		return nil
	}
	return errors.New("DeleteFailed")
}

func CreateFace(url string) (faceid, imgid string, err error) {
	requrl := fmt.Sprintf("http://apicn.faceplusplus.com/v2/detection/detect?api_key=%s&api_secret=%s&url=%s&attribute=glass,pose,gender,age,race,smiling&mode=oneface", FACEKEY, FACESECRET, url)

	resp, err := http.Get(requrl)
	if err != nil {
		return "", "", err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", "", err
	}

	type f struct {
		Face_id   string
		Position  interface{}
		Attribute interface{}
		Tag       interface{}
	}

	type r struct {
		Face       []*f
		Img_height int
		Img_id     string
		Img_width  int
		SessionId  string
		Url        string
	}

	tmp := new(r)
	tmp.Face = make([]*f, 0, 10)

	err = json.Unmarshal(buf, tmp)
	if err != nil {
		return "", "", err
	}

	if len(tmp.Face) < 1 {
		fmt.Println(string(buf))
		return "", "", errors.New(string(buf))
	}

	// fmt.Println(string(buf))
	return tmp.Face[0].Face_id, tmp.Img_id, nil
}

// 训练
func TrainVerify(personname string) (err error) {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/train/verify?api_secret=%s&api_key=%s&person_name=%s", FACESECRET, FACEKEY, personname)

	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	fmt.Println(string(b))
	return nil

}

// 给定一个Face和一个Person，返回是否是同一个人的判断以及置信度
func RecVerify(personname, faceId string) (isSamePersion bool, err error) {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/recognition/verify?api_secret=%s&face_id=%s&api_key=%s&person_name=%s", FACESECRET, faceId, FACEKEY, personname)

	resp, err := http.Get(url)
	if err != nil {
		return false, err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return false, err
	}

	type t struct {
		Confidence     float64
		Is_same_person bool
		Session_id     string
	}

	tt := new(t)

	err = json.Unmarshal(buf, tt)
	if err != nil {
		return false, err
	}
	fmt.Println(string(buf))
	return tt.Is_same_person, nil
}

// 获取详情
func GetFaceInfo(imgid string) interface{} {
	url := fmt.Sprintf("https://apicn.faceplusplus.com/v2/info/get_image?img_id=%s&api_key=%s&api_secret=%s", imgid, FACEKEY, FACESECRET)

	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	type f struct {
		Face_id   string
		Position  interface{}
		Attribute interface{}
		Tag       interface{}
	}

	type r struct {
		Face       []*f
		Img_height int
		Img_id     string
		Img_width  int
		SessionId  string
		Url        string
	}

	tmp := new(r)
	tmp.Face = make([]*f, 0, 10)

	err = json.Unmarshal(buf, tmp)
	if err != nil {
		return err
	}

	// fmt.Println(string(buf))
	return tmp
}
